package com.cxcy.swustlearner.controller;

import com.cxcy.swustlearner.pojo.StaticUser;
import com.cxcy.swustlearner.pojo.Task;
import com.cxcy.swustlearner.service.TaskService;
import common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/tasks")
public class TaskController {
    @Autowired
    private TaskService taskService;
    @PostMapping()
    public Result addTask(@RequestBody Task task) {
        if (task.getRequestpermission()>StaticUser.permission){
            return Result.fail("权限不足");
        }
        taskService.PublishTask(task,task.getRequestpermission(),StaticUser.uid);
        return Result.success("添加成功");
    }
    @GetMapping()
    public Result getTasks() {
        return Result.success(taskService.getTasks());
    }
    @GetMapping("/college")
    public Result getCollege() {
        return Result.success(taskService.selectTaskByCollegeId(StaticUser.collegeId));
    }
    @GetMapping("/class")
    public Result getClasses() {
        return Result.success(taskService.selectTaskByClassId(StaticUser.classId,StaticUser.majorId,StaticUser.collegeId));
    }
    @GetMapping("/major")
    public Result getMajors() {
        return Result.success(taskService.selectTaskByMajorId(StaticUser.majorId,StaticUser.collegeId));
    }
    @GetMapping("/mypublish")
    public Result getMyPublish() {
        return Result.success(taskService.selectTaskByPublisherId(StaticUser.uid));
    }
    @GetMapping("/mytask")
    public Result getMyTask() {
        return Result.success(taskService.selectTaskByPublisherIdAndPermissionZero(StaticUser.uid));
    }
    @DeleteMapping("/delete")
    public Result deleteTaskByid(Integer id) {
        taskService.deleteTaskByTaskId(id);
        return Result.success("删除成功");
    }
}